<?php

namespace Home\Controller;

use Think\Controller;

/**
 * Description of Error_codeController
 *
 * @author Shangyidong
 */
class ErrorcodeController extends Controller {
    //put your code here

    /**
     * 输入准确的状态码查询
     */
    function queryErrorCodeByCode() {
        header("Content-Type: text/html; charset=utf-8");
        $errorCode = I('get.errorCode');

        $Error_code = M("Error_code"); // 实例化User对象
        $error_code = $Error_code->where('code=' . $errorCode)->find();
        $result['error_code'] = $error_code;
        if (strlen($errorCode) == 3) {
            $result['level'] = "公共";
        }
        if ($error_code != null && strlen($errorCode) == 7) {//可以查询出所属类和所属方法
            //substr(string,start,length)
            $error_level = substr($errorCode, 0, 1);
            if ($error_level == "1") {
                $result['level'] = "服务级";
            } else if ($error_level == "2") {
                $result['level'] = "系统级";
            }

            $class_id = substr($errorCode, 1, 2);
            $method_id = substr($errorCode, 3, 2);
            $Controller_class = M("Controller_class"); // 实例化User对象
//            echo $errorCode.'<br/>';
//            echo $class_id;
            $controller_class = $Controller_class->where("class_id=" . intval($class_id))->find();
//            echo $Controller_class->getLastSql();
//            die();
            $result['controller_class'] = $controller_class;

            $Controller_class_method = M("Controller_class_method"); // 实例化User对象
            $controller_class_method = $Controller_class_method->where("class_id=" . intval($class_id) . " and method_id = " . intval($method_id))->find();
            $result['controller_class_method'] = $controller_class_method;
        }
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    public function queryErrorCode() {
        //echo create_uuid();
        $level = $_GET['level']; //_不填  public公共 | 服务级1  | 系统级2
        $controller_class = $_GET['controller_class'];
        $controller_class_method = $_GET['controller_class_method'];
        //$arg = $_GET['apiClassId'];
        $error_code = M();
        if ($level == "public") {
            //长度为3  公共错误码
            $sql = "select * from dy_error_code where code like '___' order by code";
            $list = $error_code->query($sql);
//            show_bug($list);
            echo json_encode($list, JSON_PRETTY_PRINT);
        } else {
            $sql = "select * from dy_error_code where code like '" . $level . $controller_class . $controller_class_method . "__' order by code";
            //echo $sql;
            $list = $error_code->query($sql);
            echo json_encode($list, JSON_PRETTY_PRINT);
        }
    }

    public function addErrorCode() {
        $code = $_GET['code'];
        $description = $_GET['description'];

        $sql = "select * from dy_error_code where code like '" . $code . "__'";
        //echo $sql.'<br/>';
        $error_code1 = M("error_code");
        $result = $error_code1->order("code")->where("code like '" . $code . "__'");
        // $total = $result->count(); //查出当前类下的方法数
        $r = $result->select();
        $result = $error_code1->order("code")->where("code like '" . $code . "__'");
        $total = $result->count(); //总记录数
        $last_code_id = $r[$total - 1]['code_id'];
        //echo $result -> getLastSql();
//        echo $total;
//        die();
        //以下算法实现code_id连续递增
        $error_code_id = $total + 1;
        //echo $total;
        if ($last_code_id > $total) {
            //echo '中间可以插入';
            for ($i = 0; $i < $total; $i++) {
//                echo $r[$i]['code_id'] . '<br/>';
                if ($r[$i]['code_id'] > $i + 1) {
                    $error_code_id = $i + 1;
                    break;
                }
            }
        }
        if ($error_code_id < 10) {
            $code = $code . '0' . $error_code_id;
        } else {
            $code = $code . "" . $error_code_id;
        }
//        echo $code;
//        die();
        $error_code = D("error_code");
        $error_code->code = $code;
        $error_code->description = $description;
        $error_code->code_id = $error_code_id;
        $result = $error_code->add();
//        echo $error_code->getLastSql();
//        die();
        if ($result > 0) {
            echo '{"info":"1","code":"' . $code . '","description":"' . $description . '"}';
        } else {
            echo '{"info":"0"}';
        }
    }

    public function deleteErrorCode() {
        $code = $_GET['code'];
        $error_code = M('error_code');
        $result = $error_code->where("code='" . $code . "'")->delete(); //与上面效果相同，也是删除id为2的数据
        echo $result;
    }

    public function editErrorCode() {
        $code = I('get.code');
        $description = I('get.description');
        $Error_code = M("Error_code"); // 实例化User对象
        // 要修改的数据对象属性赋值
        $data['description'] = $description;
        $result = $Error_code->where('code=' . $code)->save($data); // 根据条件保存修改的数据
        echo $result;
    }

}
